package ding.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/hello.action")
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取前端传递的参数
        String name = req.getParameter("name");
        System.out.println("接收到前端参数------" + name);
        System.out.println("进入Servlet资源");

        req.setAttribute("hello", "req:已进入HelloServlet资源");

        HttpSession session = req.getSession();
        session.setAttribute("hello", "session:已进入HelloServlet资源");

//        req.getRequestDispatcher("/hello.jsp").forward(req,resp);

//        resp.getWriter().println("已进入HelloServlet资源");
        //重定向
        resp.sendRedirect("hello.jsp");
    }
}
